import {createRouter, createWebHistory} from 'vue-router'
import {loadingBar} from "heyui";

const groupRoutes = [
	{
		path: '/login',
		name: 'Login',
		component: () => import('@components/Login')
	},
	{
		path: '/',
		name: 'AppFrame',
		component: () => import('@components/app/AppFrame'),
		children: [
			{
				path: '',
				name: 'DashboardMain',
				component: () => import('@components/group/dashboard/DashboardMain'),
				meta: {title: '桌面', icon: 'icon-monitor'}
			},
			{
				path: 'merchant/info',
				name: 'MerchantInfo',
				component: () => import('@components/group/setting/merchant/MerchantInfo'),
				meta: {title: '商户信息', icon: 'icon-monitor'}
			},
			{
				path: 'admin',
				name: 'AdminList',
				component: () => import('@components/group/setting/admin/AdminList'),
				meta: {title: '用户管理', icon: 'icon-monitor'}
			},
			{
				path: 'role',
				name: 'RoleList',
				component: () => import('@components/group/setting/role/RoleList'),
				meta: {title: '角色', icon: 'icon-monitor'}
			},
			{
				path: 'organization',
				name: 'Organization',
				component: () => import('@components/group/setting/organization/OrganizationList'),
				meta: {title: '组织机构', icon: 'icon-monitor'}
			},
			{
				path: 'account',
				name: 'AccountBasic',
				component: () => import('@components/common/AccountBasic'),
				meta: {title: '账号信息', icon: 'icon-monitor'}
			},
			{
				path: 'project',
				name: 'Project',
				component: () => import('@components/group/todolist/project/ProjectList'),
				meta: {title: '项目管理', icon: 'icon-monitor'}
			},
			{
				path: 'todolist',
				name: 'TodoList',
				component: () => import('@components/group/todolist/todolist/TodoList'),
				meta: {title: '代办任务', icon: 'icon-monitor'}
			}
		]
	}, {
		path: '/permission/error',
		name: 'PermissionError',
		component: () => import('@components/common/PermissionError')
	}
]


const routes = [
	{
		path: '/login',
		name: 'Login',
		component: () => import('@components/Login')
	},
	{
		path: '/',
		name: 'AppFrame',
		component: () => import('@components/app/AppFrame'),
		children: [
			{
				path: '',
				name: 'DashboardMain',
				component: () => import('@components/organization/dashboard/DashboardMain'),
				meta: {title: '桌面', icon: 'icon-monitor'}
			},
		]
	}, {
		path: '/permission/error',
		name: 'PermissionError',
		component: () => import('@components/common/PermissionError')
	}
]

/**
 * 创建路由
 * @param isGroup
 * @returns {Router}
 */
const create = (isGroup = false) => {
	let router = createRouter({
		history: createWebHistory(process.env.BASE_URL),
		routes: isGroup ? groupRoutes : routes
	})

	let isFirstRouter = true;

	router.beforeEach((to, from, next) => {
		loadingBar.start();
		if (to.meta && to.meta.title) {
			document.title = to.meta.title + ' - 代办清单';
		} else {
			document.title = '代办清单';
		}
		isFirstRouter = false;
		next();
	});

	router.afterEach(() => {
		loadingBar.success();
		document.documentElement.scrollTop = 0;
		document.body.scrollTop = 0;
		let layoutContent = document.querySelector('.h-layout-content');
		if (layoutContent) {
			layoutContent.scrollTop = 0;
		}
	});
	return router;
}

export default create
